/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.business.tracking;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.vineatoum.business.state.abstraction.VinAbstract;

public class TrackInfo {
	private VinAbstract trackedElement;
	private List<TrackInfo> fromElements = new ArrayList<TrackInfo>();
	private List<TrackInfo> childElements = new ArrayList<TrackInfo>();

	private List<ITrackableElement> listedTracking = new ArrayList<ITrackableElement>();

	public List<ITrackableElement> getListedTracking() {

		Collections.sort(listedTracking);
		return listedTracking;
	}

	public void addFromElements(TrackInfo a) {
		if (!fromElements.contains(a)) {
			fromElements.add(a);
			getListedTracking().add(a.getTrackedElement());
		}
	}

	public void addChildElements(TrackInfo a) {
		if (!childElements.contains(a)) {
			childElements.add(a);
			getListedTracking().add(a.getTrackedElement());
		}
	}

	public void setListedTracking(List<ITrackableElement> listedTracking) {
		this.listedTracking = listedTracking;
	}

	public VinAbstract getTrackedElement() {
		return trackedElement;
	}

	public void setTrackedElement(VinAbstract trackedElement) {
		this.trackedElement = trackedElement;
	}

	public List<TrackInfo> getFromElements() {
		return fromElements;
	}

	public void setFromElements(List<TrackInfo> fromElements) {
		this.fromElements = fromElements;
	}

	public List<TrackInfo> getChildElements() {
		return childElements;
	}

	public void setChildElements(List<TrackInfo> childElements) {
		this.childElements = childElements;
	}

	public boolean isFirstElement() {
		return this.fromElements.isEmpty();
	}

	public boolean isLastElement() {
		return this.childElements.isEmpty();
	}

	@Override
	public String toString() {
		String returnString = "";
		returnString += "=== Tracking de " + this.trackedElement + " ===\n";
		for (Iterator<ITrackableElement> iterator = listedTracking.iterator(); iterator
				.hasNext();) {
			ITrackableElement e = iterator.next();
			String me = "";
			if (e == this.trackedElement) {
				me = "It's me ! : ";
			}
			returnString += me + e + "\n";
		}

		return returnString;

	}

}
